package com.compass.doctor.ui.prescription.adapter

import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View.OnFocusChangeListener
import android.view.ViewGroup
import com.compass.doctor.callback.OnCustomButtonClick
import com.compass.doctor.databinding.ItemInventoryMedicineBinding
import com.compass.framework.adapter.BaseBindViewHolder
import com.compass.framework.adapter.BaseRecyclerViewAdapter
import com.compass.framework.ext.gone
import com.compass.framework.ext.onClick
import com.compass.framework.manager.DictionariesManager
import com.compass.framework.model.Drug
import com.compass.framework.model.Products

/**
 * Description： 中药adapter
 * Date:2024/10/21 9:19
 */
class MedicineInventoryAdapter :
    BaseRecyclerViewAdapter<Drug, ItemInventoryMedicineBinding>() {
    private var onMedicineTextChange: OnMedicineTextChange? = null

    private var onCustomBottomClick: OnCustomButtonClick? = null
    fun setCustomBottomClick(onChatBottomClick: OnCustomButtonClick) {
        this.onCustomBottomClick = onChatBottomClick
    }

    override fun getViewBinding(
        layoutInflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): ItemInventoryMedicineBinding {
        return ItemInventoryMedicineBinding.inflate(layoutInflater, parent, false)
    }

    override fun onBindDefViewHolder(
        holder: BaseBindViewHolder<ItemInventoryMedicineBinding>,
        item: Drug?,
        position: Int
    ) {
        if (item == null) return
        holder.binding.apply {
            tvName.text = item.name
            tvAttribute.text = item.getRemarkString()

            tvNum.setText(item.num)

            tvPlusSign.onClick {
                onCustomBottomClick?.onButtonCallBack(position, "plusSign")
            }
            tvMinusSign.onClick {
                onCustomBottomClick?.onButtonCallBack(position, "MinusSign")
            }
            tvDelete.onClick {
                onCustomBottomClick?.onButtonCallBack(position, "delete")
            }


            val gramNumberWatcher: TextWatcher = object : TextWatcher {
                override fun beforeTextChanged(
                    s: CharSequence,
                    start: Int,
                    count: Int,
                    after: Int
                ) {
                }

                override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                }

                override fun afterTextChanged(s: Editable) {
                    val trim: String = tvNum.getText().toString().trim()
                    if (!TextUtils.isEmpty(trim)) {
                        item.num=trim
                    } else {
                        item.num="0"
                        tvNum.setText(item.num)
                    }
                    onMedicineTextChange!!.onGramChange()
                }
            }
            tvNum.onFocusChangeListener = OnFocusChangeListener { v, hasFocus ->
                if (hasFocus) {
                    tvNum.addTextChangedListener(gramNumberWatcher)
                } else {
                    tvNum.removeTextChangedListener(gramNumberWatcher)
                }
            }
        }
    }


    fun setOnMedicineTextChange(onMedicineTextChange: OnMedicineTextChange) {
        this.onMedicineTextChange = onMedicineTextChange
    }

    interface OnMedicineTextChange {
        /**
         * 克数变化监听
         */
        fun onGramChange()
    }
}
